package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;

import java.util.List;
import java.util.Map;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Menu> findMenuAll() {
      return   menuMapper.findMenuAll();

    }

    @Override
    public List<Integer> findRoleAndMenuByRoleId(Integer id) {
        return menuMapper.findMenuByRoleId(id);
    }

    @Override
    public List<Map> findMenuByUserLevel(String userName) {
//        User user = userMapper.findUserByUserName(userName);

//        List<Map<String,Object>> menuList = menuMapper.findMenuByUserLevel(user.getId());

        return null;
    }
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage, pageSize);
        List<Menu> menuList = menuMapper.findPage(queryString);
        PageInfo<Menu> menuPageInfo = new PageInfo<>(menuList);

        return new PageResult(menuPageInfo.getTotal(), menuPageInfo.getList());

    }

    @Override
    public void addMenu(Menu menu) {
        menuMapper.addMenu(menu);
    }


    @Override
    public void deleteMenuById(Integer id) {
//        检查是否被引用
        Integer count = menuMapper.findMenuAndRoleByMenuId(id);

        if (count > 0) {
            throw new RuntimeException("该菜单被引用，无法删除");
        }

        menuMapper.deleteMenuById(id);
    }

    @Override
    public Menu findMenuById(Integer id) {
        return menuMapper.findMenuById(id);
    }


    @Override
    public List<Menu> findMenuByUserName(String name) {
        List<Menu> menuList = menuMapper.findMenuByUserName(name);
        return menuList;
    }

    @Override
    public void updateMenu(Menu menu) {

        Integer id = menuMapper.findMenuIdByMenuParentMenuId(menu.getParentMenuId());
        menu.setParentMenuId(id);

        menuMapper.updateMenu(menu);
    }
}

/***
 *                    .::::.
 *                  .::::::::.
 *                 ::::::::::: 
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */